﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


using PowerCreator.LMS.Paper.Entiry;
using PowerCreator.LMS.Web.Paper;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common.Utils;


public partial class sysadmin_QuestionManager_EditPaperInfo :PowerCreator.LMS.Web.Base.BasePage
{

    ManagerPaper objManagerPaper = new ManagerPaper();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //BinderDDL();
            ViewState["PaperID"] = Request.QueryString["p"].ToString();
            //ViewState["PaperSelectType"] = Base64.Decode(Request.QueryString["py"].ToString());
            ViewState["UserID"] = 10000;
            Pageload();
            InitPage();
        }
    }
    protected override void InitPage()
    {
        lab_TitleInfo.Text = GetLanageValueByKey("lab_AddPaperInfo_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_PaperName.Text = GetLanageValueByKey("lab_AddPaperInfo_PaperName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_PaperTypeTitle.Text = GetLanageValueByKey("lab_AddPaperInfo_PaperType", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_DefaultScore.Text = GetLanageValueByKey("lab_AddPaperInfo_PaperDefaultPassScore", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_PaperDiffculty.Text = GetLanageValueByKey("lab_AddPaperInfo_PaperDiffculty", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_SelectType.Text = GetLanageValueByKey("lab_AddPaperInfo_PaperSelectType", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_TestTime.Text = GetLanageValueByKey("lab_AddPaperInfo_PaperTestTime", PowerCreator.LMS.Language.LanagetItemType.Page);

        rdbtn_Defoult.Text = GetLanageValueByKey("lab_AddPaperInfo_CommonPaper", PowerCreator.LMS.Language.LanagetItemType.Page);
        rdbtn_Test.Text = GetLanageValueByKey("lab_AddPaperInfo_TestPaper", PowerCreator.LMS.Language.LanagetItemType.Page);

        btnSaveNext.Text = GetLanageValueByKey("btn_AddPaperInfo_SavePaper", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_CheckQuestion.Text = GetLanageValueByKey("btn_AddPaperInfo_SelectQuestion", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }
    private void Pageload()
    {
        //PaperInfo info = new PaperInfo();
        VOPaper info = new VOPaper();
        info = objManagerPaper.LoadPaper(int.Parse(ViewState["PaperID"].ToString()));
        ViewState["PaperInfo"] = info;
        txtPaperName.Text = info.PaperName;
        txtPassScore.Text = info.PaperPassScore.ToString();
        txtTestTime.Text = info.PaperTestTime.ToString();
        ddlDiffculty.SelectedValue = info.PaperDiffculty.ToString();
        if (info.PaperType!= 1)
        {
            lab_PaperType.Text = GetLanageValueByKey("lab_AddPaperInfo_CommonPaper", PowerCreator.LMS.Language.LanagetItemType.Page);
            txtTestTime.Enabled = true;
            //rdbtn_Defoult.Checked = true;
        }
        else
        {
            lab_PaperType.Text = GetLanageValueByKey("lab_AddPaperInfo_TestPaper", PowerCreator.LMS.Language.LanagetItemType.Page);
            //rdbtn_Test.Checked = true;
            txtTestTime.Enabled = false;
        }
        if (info.PaperSelectType == 1)
        {
            lab_SelType.Text = GetLanageValueByKey("Msg_AddPaperInfo_SelectQuestionType1", PowerCreator.LMS.Language.LanagetItemType.Page);
        }
        else if(info.PaperSelectType == 2)
        {
            lab_SelType.Text = GetLanageValueByKey("Msg_AddPaperInfo_SelectQuestionType2", PowerCreator.LMS.Language.LanagetItemType.Page);
            if (info.PaperCelueID != -1)
            {
                btn_CheckQuestion.Visible = false;
               
            }
            else
            {
                btn_CheckQuestion.Visible = true;
            }
            return;
        }

        string search = "FKPaperQuestionPaperID=" + ViewState["PaperID"].ToString();
        if (objManagerPaper.QueryPaperQuestion(search).Rows.Count == 0)
        {
            btn_CheckQuestion.Visible = true;
        }
        else
        {
            btn_CheckQuestion.Visible = false;
        }

        rfv_txtUserName.ErrorMessage = GetLanageValueByKey("Msg_AddPaperInfo_checkmsg1", PowerCreator.LMS.Language.LanagetItemType.Page);
        rfv_PassScore.ErrorMessage = GetLanageValueByKey("Msg_AddPaperInfo_checkmsg2", PowerCreator.LMS.Language.LanagetItemType.Page);
        

    }

    //private void BinderDDL()
    //{
    //    DataTable dt = new DataTable();
    //    dt = userManager.Query("");
    //    ddlUserList.DataSource = dt;
    //    ddlUserList.DataTextField = "UserName";
    //    ddlUserList.DataValueField = "UserID";
    //    ddlUserList.DataBind();
    //}
    private bool PageCheck()
    {
        if (rdbtn_Defoult.Checked)
        {
            if (string.IsNullOrEmpty(txtPaperName.Text.Trim()) | string.IsNullOrEmpty(txtPassScore.Text.Trim()) | string.IsNullOrEmpty(txtTestTime.Text.Trim()))
            {
                Script.Alert(this, "msg", GetLanageValueByKey("Msg_AddPaperInfo_checkmsg4", PowerCreator.LMS.Language.LanagetItemType.Page));
                return false;
            }
            if (!PageValidate.IsNumber(txtPassScore.Text.Trim()) | !PageValidate.IsNumber(txtTestTime.Text.Trim()))
            {
                Script.Alert(this, "msg", GetLanageValueByKey("Msg_AddPaperInfo_checkmsg5", PowerCreator.LMS.Language.LanagetItemType.Page));
                return false;
            }

        }
        else
        {
            if (string.IsNullOrEmpty(txtPaperName.Text.Trim()))
            {
                Script.Alert(this, "msg", GetLanageValueByKey("Msg_AddPaperInfo_checkmsg1", PowerCreator.LMS.Language.LanagetItemType.Page));
                return false;
            }
            if (!string.IsNullOrEmpty(txtPassScore.Text.Trim()))
            {
                if (!PageValidate.IsNumber(txtPassScore.Text.Trim()))
                {
                    Script.Alert(this, "msg", GetLanageValueByKey("Msg_AddPaperInfo_checkmsg2", PowerCreator.LMS.Language.LanagetItemType.Page));
                    return false;
                }
            }
        }
        string search = " PaperName=" + "'" + txtPaperName.Text + "'";
        if (objManagerPaper.CountPaper(search) > 1)
        {
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_AddPaperInfo_checkmsg6", PowerCreator.LMS.Language.LanagetItemType.Page));
            return false;

        }
        return true;

    }
    protected void btnSaveNext_Click(object sender, EventArgs e)
    {
        if (!PageCheck())
        {
            return;
        }


        VOPaper info = (VOPaper)ViewState["PaperInfo"];

        info.PaperCreateID = int.Parse(ViewState["UserID"].ToString());
        info.PaperCreateTime = DateTime.Now;
        info.PaperPassScore = int.Parse(txtPassScore.Text);
        info.PaperShowAnswer = 2;
        info.PaperShowScore = 2;
        info.PaperMarkID = int.Parse(ViewState["UserID"].ToString());
        info.PaperName = txtPaperName.Text;
        info.PaperDiffculty = int.Parse(ddlDiffculty.SelectedValue.ToString());
        if (rdbtn_Defoult.Checked)
        {
            info.PaperTestTime = int.Parse(txtTestTime.Text);
        }


        if (objManagerPaper.UpdatePaper(info))
        {
            Script.Alert(this, "msg", "操作成功");
           
        }
        else
        {
            Script.Alert(this, "msg", "操作失败");
            
        }
        //Script.Alert(Page, "msg", manager.Update(info));
        //string url = "";
        //int urltype = int.Parse(ddlQuestionSelectType.SelectedValue);
        //if (urltype == 1)
        //{
        //    url = "SelectQuestion.aspx?p=" + Base64.Encode(ViewState["PaperID"].ToString());
        //}
        //else
        //{
        //    url = "";
        //}
        //Script.RedirectURL(url);
    }

    protected void btn_CheckQuestion_Click(object sender, EventArgs e)
    {
        string url = string.Empty;
        if (lab_SelType.Text == "人工选题")
        {
            url = "SelectQuestion.aspx?p=" + ViewState["PaperID"].ToString();
        }
        else if (lab_SelType.Text == "自动选题")
        {
            url = "AoutSelectQuestion.aspx?p=" + ViewState["PaperID"].ToString();
        }
        else
        {
            url = "CelueSelectQuestion.aspx?p=" + ViewState["PaperID"].ToString();
        }
        Script.RedirectURL(url);

    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Script.RedirectURL("ManagerPaperInfo.aspx");
    }
    protected void rdbtn_Defoult_CheckedChanged(object sender, EventArgs e)
    {
        
        txtTestTime.Enabled = true;
        txtPassScore.Enabled = true;
    }
    protected void rdbtn_Test_CheckedChanged(object sender, EventArgs e)
    {
        txtTestTime.Enabled = false;
        txtPassScore.Enabled = false;
    }
}
